﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XLoader;
using OpenTK;
using Ignitron.EEngine;
using Ignitron.EEngine.Maths;
using Ignitron.EEngine.Model;
using System.Windows.Media;

namespace Board3D.Models
{
    /// <summary>
    /// Helper class for converting an XLoader model to the EEngine model
    /// </summary>
    public static class ModelConverter
    {
        /// <summary>
        /// Converts the model
        /// </summary>
        public static EModel ToEE(this GLModel model, Engine engine)
        {
            EModel eModel = engine.CreateModel();

            foreach (GLMesh mesh in model.Meshes)
            {
                EMesh eMesh = eModel.AddMesh();
                eMesh.SetData(
                    mesh.Vectors.ToEE(),
                    mesh.Normals.ToEE(),
                    mesh.TexCoords.ToEE(),
                    mesh.Indices.ToArray());
            }

            return eModel;
        }

        /// <summary>
        /// Converts the array of Vector3
        /// </summary>
        public static EVector3[] ToEE(this Vector3d[] vecArray)
        {
            return vecArray.Select(v => v.ToEE()).ToArray();
        }

        /// <summary>
        /// Converts the Array of Vector2
        /// </summary>
        public static EVector2[] ToEE(this Vector2d[] vecArray)
        {
            return vecArray.Select(v => v.ToEE()).ToArray();
        }

        /// <summary>
        /// Converts Vector3
        /// </summary>
        public static EVector3 ToEE(this Vector3d vec)
        {
            return new EVector3((float)vec.X, (float)vec.Y, (float)vec.Z);
        }

        /// <summary>
        /// Converts Vector2
        /// </summary>
        public static EVector2 ToEE(this Vector2d vec)
        {
            return new EVector2((float)vec.X, (float)vec.Y);
        }

        /// <summary>
        /// Converts color to EVector4 representation
        /// </summary>
        public static EVector4 ToEE(this Color color)
        {
            return new EVector4(color.R / 255f, color.G / 255f, color.B / 255f, color.A / 255f);
        }

    }
}
